<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\Medium\Action\Dashboard\Traits;

trait MediumTrait
{
    /**
     * 文件大小.
     * @return int ...
     */
    public function size(array $content): int
    {
        $size = 0;
        empty($content['voicePath']) || $size = $this->getFileSize($content['voicePath']);
        empty($content['videoPath']) || $size = $this->getFileSize($content['videoPath']);
        empty($content['imagePath']) || $size = $this->getFileSize($content['imagePath']);
        empty($content['filePath']) || $size = $this->getFileSize($content['filePath']);
        return $size;
    }

    private function getFileSize($name): int
    {
        // 这种实现方式性能不好，需要改为上传时计算
        $size = 0;
        try {
            $content = file_get_contents(file_full_url($name));
            $size = strlen($content);
        } catch (\Throwable $e) {
        }

        return $size;
    }
}
